package com.dolphin.servicedriveruser.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dolphin.common.constant.CommonStatusEnum;
import com.dolphin.common.constant.DriverCarConstants;
import com.dolphin.common.dto.DriverCarBindingRelationship;
import com.dolphin.common.dto.DriverUser;
import com.dolphin.common.dto.ResponseResult;
import com.dolphin.servicedriveruser.mapper.DriverCarBindingRelationshipMapper;
import com.dolphin.servicedriveruser.mapper.DriverUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DriverCarBindingRelationshipService {
    @Autowired
    private DriverCarBindingRelationshipMapper driverCarBindingRelationshipMapper;
    @Autowired
    private DriverUserMapper driverUserMapper;
    public ResponseResult bind(DriverCarBindingRelationship driverCarBindingRelationship) {
        // 判断，如果参数中的车辆和司机已经绑定过了，则不允许再次绑定
        LambdaQueryWrapper <DriverCarBindingRelationship> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DriverCarBindingRelationship::getCarId,driverCarBindingRelationship.getCarId());
        queryWrapper.eq(DriverCarBindingRelationship::getDriverId,driverCarBindingRelationship.getDriverId());
        queryWrapper.eq(DriverCarBindingRelationship::getBindState,DriverCarConstants.DRIVER_CAR_BIND);
        Integer bindCount = driverCarBindingRelationshipMapper.selectCount(queryWrapper);
        if (bindCount.intValue() > 0) {
            return ResponseResult.fail(CommonStatusEnum.DRIVER_CAR_BIND_EXISTS.getCode(),CommonStatusEnum.DRIVER_CAR_BIND_EXISTS.getValue());
        }

        // 司机被绑定了
        queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DriverCarBindingRelationship::getDriverId,driverCarBindingRelationship.getDriverId());
        queryWrapper.eq(DriverCarBindingRelationship::getBindState,DriverCarConstants.DRIVER_CAR_BIND);
        Integer driverBindCount = driverCarBindingRelationshipMapper.selectCount(queryWrapper);
        if (driverBindCount.intValue() > 0) {
            return ResponseResult.fail(CommonStatusEnum.DRIVER_BIND_EXISTS.getCode(),CommonStatusEnum.DRIVER_BIND_EXISTS.getValue());
        }

        //车辆被绑定了
        queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DriverCarBindingRelationship::getCarId,driverCarBindingRelationship.getCarId());
        queryWrapper.eq(DriverCarBindingRelationship::getBindState,DriverCarConstants.DRIVER_CAR_BIND);
        Integer carBindCount = driverCarBindingRelationshipMapper.selectCount(queryWrapper);
        if (carBindCount.intValue() > 0) {
            return ResponseResult.fail(CommonStatusEnum.CAR_BIND_EXISTS.getCode(),CommonStatusEnum.CAR_BIND_EXISTS.getValue());
        }



        LocalDateTime now = LocalDateTime.now();
        driverCarBindingRelationship.setBindingTime(now);
        driverCarBindingRelationship.setBindState(DriverCarConstants.DRIVER_CAR_BIND);

        driverCarBindingRelationshipMapper.insert(driverCarBindingRelationship);
        return ResponseResult.success();
    }

    public ResponseResult unbind(DriverCarBindingRelationship driverCarBindingRelationship) {
        LambdaQueryWrapper <DriverCarBindingRelationship> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DriverCarBindingRelationship::getCarId,driverCarBindingRelationship.getCarId());
        queryWrapper.eq(DriverCarBindingRelationship::getDriverId,driverCarBindingRelationship.getDriverId());
        queryWrapper.eq(DriverCarBindingRelationship::getBindState,DriverCarConstants.DRIVER_CAR_BIND);
        List<DriverCarBindingRelationship> driverCarBindingRelationships = driverCarBindingRelationshipMapper.selectList(queryWrapper);
        // 判断司机和车辆是否绑定关系存在
        if (driverCarBindingRelationships.isEmpty()) {
            return ResponseResult.fail(CommonStatusEnum.DRIVER_CAR_BIND_NOT_EXISTS.getCode(),CommonStatusEnum.DRIVER_CAR_BIND_NOT_EXISTS.getValue());
        }
        DriverCarBindingRelationship relationship = driverCarBindingRelationships.get(0);
        relationship.setUnBindingTime(LocalDateTime.now());
        relationship.setBindState(DriverCarConstants.DRIVER_CAR_UNBIND);
        driverCarBindingRelationshipMapper.updateById(relationship);
        return ResponseResult.success();

    }

    public ResponseResult<DriverCarBindingRelationship> getDriverCarBindingRelationshipByDriverPhone(String driverPhone) {
        LambdaQueryWrapper <DriverUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DriverUser::getDriverPhone,driverPhone);
        DriverUser driverUser = driverUserMapper.selectOne(queryWrapper);
        Long driverId = driverUser.getId();

        LambdaQueryWrapper <DriverCarBindingRelationship> driverCarBindingRelationshipLambdaQueryWrapper = new LambdaQueryWrapper<>();
        driverCarBindingRelationshipLambdaQueryWrapper.eq(DriverCarBindingRelationship::getDriverId,driverId);
        driverCarBindingRelationshipLambdaQueryWrapper.eq(DriverCarBindingRelationship::getBindState,DriverCarConstants.DRIVER_CAR_BIND);

        DriverCarBindingRelationship driverCarBindingRelationship = driverCarBindingRelationshipMapper.selectOne(driverCarBindingRelationshipLambdaQueryWrapper);
        return ResponseResult.success(driverCarBindingRelationship);

    }
}
